package ar.uba.dc.tl.mylanga.semantic;

import ar.uba.dc.tl.mylanga.exception.DuplicateFunctionException;
import ar.uba.dc.tl.mylanga.node.AFunctionDefinition;
import ar.uba.dc.tl.mylanga.node.PProgram;
import ar.uba.dc.tl.mylanga.symbol.Symbol;
import ar.uba.dc.tl.mylanga.symbol.SymbolFactory;
import ar.uba.dc.tl.mylanga.symbol.SymbolTable;

public class DuplicateFunctionRule extends SemanticRule {

    private SymbolTable symbolTable;
    private SymbolFactory symbolFactory;

    public DuplicateFunctionRule(PProgram program) {
        super("DuplicateFunctionRule", program);

    }

    @Override
    public void check() {
        this.symbolTable = new SymbolTable();
        this.symbolFactory = new SymbolFactory();

        this.getProgram().apply(this);
    }

    @Override
    public void caseAFunctionDefinition(AFunctionDefinition node) {
        super.caseAFunctionDefinition(node);

        Symbol symbol = symbolFactory.get(node.getName().getText());

        if (symbolTable.contains(symbol)) {
            throw new DuplicateFunctionException(node.getName());
        }

        symbolTable.put(symbol);
    }

}
